package com.glory.cqaospda.base

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Message
import com.glory.cqaospda.view.LoginActivity
import com.glory.mvvmnet.utils.InitUtils
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_InitUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.*
import java.io.File
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author HX
 * @date 2021/5/21
 * @desc
 */
class MyApp : Application() {

    private val timer = Timer()
    private val TINE_TASK_AN_HOUR = 60 * 1000L
    private var task: TimerTask? = null

    var handler: Handler? = null
    var handler1: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            when(msg.what) {
                5486 -> {
                    Constant.time = FreeApi_DateUtils.getNowStringDate("HHmm")
                    FreeApi_LogUtils.saveErrorMessages( Constant.time,"handler发送")

                    if (Constant.loginTime.toInt() >= "0730".toInt()
                        && Constant.loginTime.toInt() <= "0815".toInt()
                    ) {

                    } else {
                        if (Constant.time == "0815") {
                            FreeApi_ActivityManager.getActivityManager().finishAllActivity()
                            val intent = Intent(this@MyApp, LoginActivity::class.java)
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            startActivity(intent)
                        }
                    }

                    if (Constant.loginTime.toInt() >= "2010".toInt()
                        && Constant.loginTime.toInt() <= "2015".toInt()
                    ) {

                    } else {
                        if (Constant.time == "2015") {
                            FreeApi_ActivityManager.getActivityManager().finishAllActivity()
                            val intent = Intent(this@MyApp, LoginActivity::class.java)
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            startActivity(intent)
                        }
                    }
                }
            }
            super.handleMessage(msg)
        }
    }
    override fun onCreate() {
        super.onCreate()
        InitUtils.init(this)
        Constant.mUrl = FreeApi_SharePreferencesUtils.getSharePre("SystemSet", "sercerUrl", Constant.mUrl,applicationContext)
        FreeUI_InitUtils.init(applicationContext, this, true, 180 * 1000L, 0)
        deleteLog()
        app = applicationContext
        val sharePre = FreeApi_SharePreferencesUtils.getSharePre(
            "SystemSet",
            "connectTimeout",
            applicationContext
        )
        Constant.fileUrl = FreeApi_SharePreferencesUtils.getSharePre(
            "SystemSet",
            "fileUrl",
            applicationContext)
        val isUpdate = FreeApi_SharePreferencesUtils.getSharePre(
            "SystemSet",
            "isUpdate",
            applicationContext)
        Constant.isUpdate = isUpdate.equals("Y")
        if (!sharePre.equals("")){
            Constant.connectTimeout =sharePre.toInt()
        }
        Handler().postDelayed({ showTimer() }, TINE_TASK_AN_HOUR)
    }

    private fun showTimer() {
        task = object : TimerTask() {
            override fun run() {
                val message = Message()
                message.what = 5486
                handler1.sendMessage(message)
            }
        }
        timer.schedule(task, Date(), TINE_TASK_AN_HOUR)
    }

    companion object {
        @JvmField
        var app: Context? = null

        var instance: MyApp? = null
            get() {
                if (field == null) {
                    field = MyApp()
                }
                return field
            }
            private set
    }

    fun deleteLog() {
        val file = File(FreeApi_StaticMembers.logRootPath)
        val array = ArrayList<String>()
        for (i in 1 until 30){
            val day = FreeApi_DateUtils.getOldDate(i)
            array.add("crash$day.log")
        }
        val day = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(Date())
        array.add("crash$day.log")
        if (file.exists()) {
            val fileArray = file.listFiles()
            if (fileArray != null) {
                for (i in fileArray.indices) {
                    val logFile = fileArray[i]
                    var isOutTime = true
                    if (array.contains(logFile.name)) {
                        isOutTime = false
                    }
                    if (isOutTime) {
                        logFile.delete()
                    }
                }
            }
        }
    }

}